import mongoose from 'mongoose';
import config from './index.js';

const connectDB = async () => {
  try {
    await mongoose.connect(config.db.uri, config.db.options);
    console.log('MongoDB connected');
    
    // 开发环境显示查询日志
    if (config.app.env === 'development') {
      mongoose.set('debug', (coll, method, query, doc) => {
        console.log(`MongoDB: ${coll}.${method}`, JSON.stringify(query));
      });
    }
  } catch (err) {
    console.error('MongoDB connection error:', err);
    process.exit(1);
  }
};

export default connectDB;